package com.base.look.adapter;


import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.base.commonlib.utils.GlideUtil;
import com.base.look.R;
import com.bumptech.glide.Glide;
import com.ycbjie.zoomimagelib.listener.OnZoomClickListener;
import com.ycbjie.zoomimagelib.view.ZoomImageView;

import java.util.ArrayList;
import java.util.List;

public class SnapAdapter extends RecyclerView.Adapter <SnapAdapter.MyViewHolder>{


    private Context mContext;
    private Activity activity;
    public SnapAdapter(Context context,Activity activity){
        this.mContext =context;
        this.activity =activity;
    }

    private List<String> urlList = new ArrayList<>();
    public void setData(List<String> list) {
        urlList.clear();
        this.urlList = list;
    }

    public List<String> getData() {
        return urlList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_snap, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        if (urlList == null || urlList.isEmpty())
            return;
        //String url = urlList.get(position%urlList.size());
        //Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), url);
         GlideUtil.load(mContext,urlList.get(position),holder.imageView);
        //Glide.with(mContext).load(urlList.get(position)).into(holder.imageView).onLoadFailed(new Exception(),mContext.getResources().getDrawable(R.drawable.ic_empty));
        holder.imageView.setOnZoomClickListener(new OnZoomClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
        //holder.imageView.setBackgroundResource(url);
    }


    class MyViewHolder extends RecyclerView.ViewHolder{
        ZoomImageView imageView;
        MyViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_image);
        }

    }

    @Override
    public int getItemCount() {
        /*if (urlList.size() != 1) {
            Log.e("getItemCount","getItemCount---------");
            return Integer.MAX_VALUE; // 无限轮播
        } else {
            Log.e("getItemCount","getItemCount++++----");
            return urlList.size();
        }*/

        return urlList!=null ? urlList.size() : 0;
    }

}
